/////////////
//	DDBtimap.cpp	:	v0010
//	Written by		:	Li Haijun
//	Compiler		:	Microsoft Visual C++ 4.0 & DirectX
//	Library			:	DDraw.Lib
//	Copyright (C)	:	1996 WayAhead Corporation 
//	v0010			:	Nov.28.1996
/////////////
// implementation file
// to show the special bitmap . eg : tq.bmp fx.bmp sz.bmp
// This file provide basic interfaces for deal with bitmap of interface 
//////////////////////////////

#include	"stdafx.h"

#include	<ddraw.h>
#include	"Assert.h"
#include	"DDCompo.h"

#include	"marco.h"
#include	"DDBitmap.h"

////////////////////////////
#include "l_allbmp.h"
//extern char	bmpfilename[30];
//extern char	listfilename[30];
/////////////////////////////////////

/////////////////////////////////////
class	CDDBitmap	*	pAllBitmap[MAX_BITMAP];		//to store all bitmap's pointer 
/////////////////////////////////////
//	to restore all button's pointer
//constructor
CDDBitmap::CDDBitmap()
{
	m_szOneBitmap.cx = 0L;
	m_szOneBitmap.cy = 0L;

	m_rTotal.left	= 0L;
	m_rTotal.top	= 0L;
	m_rTotal.right	= 0L;
	m_rTotal.bottom	= 0L;

	m_rCurrent.left	= 0L;
	m_rCurrent.top	= 0L;
	m_rCurrent.right	= 0L;
	m_rCurrent.bottom	= 0L;

	m_dwX = 0;	m_dwY = 0;
	
	m_nID = 0;
	m_nState = 0;
	m_nPart = 0;
}
//constructor
CDDBitmap::~CDDBitmap()
{
	Release();
}
// to preload this bitmap file data by filename id
BOOL CDDBitmap::PreLoad( int FilenameId, int x, int y,
						int OneBitmapWidth/*=0*/, int OneBitmapHeight/*=0*/, 
						const char* bmpfilename/* ="bmp\\bmp.lwc"*/, const char* listfilename/* = "bmp\\bmp.idx"*/ )
{
	char	fname[20];

	HRESULT			ddrval;
	DDSURFACEDESC	ddsd;

	// tempory add this line for debug
	m_nID = FilenameId ;

//////////////////////////////////////////////////
	_itoa( FilenameId, fname, 10 );

	class CPicture_imageall picture;

	picture.image_open_compress(bmpfilename);
	picture.image_open_index(listfilename);
	picture.LoadBitmap( &m_BitmapSurface, FilenameId );
	picture.image_close_index();
	picture.image_close_compress();

/////////////////////////////////////////////////////////
    //
    // get size of surface.
    //
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;

    ddrval = m_BitmapSurface.GetSurface()->GetSurfaceDesc(&ddsd);
	if(ddrval != DD_OK)
		return FALSE;
	m_rTotal.left = m_rTotal.top = 0;
	m_rTotal.right = ddsd.dwWidth;
	m_rTotal.bottom = ddsd.dwHeight;
// to set the value of m_szOneBitmap
	if( (OneBitmapWidth==0)&&(OneBitmapHeight==0) )
	{
		// I assume that all single bitmaps' height is equal to it's width
		if(m_rTotal.bottom > m_rTotal.right)
			m_szOneBitmap.cx = m_szOneBitmap.cy = m_rTotal.right;
		else
			m_szOneBitmap.cx = m_szOneBitmap.cy = m_rTotal.bottom;
		// to set the value of m_rCurrent
		m_rCurrent.left = m_rCurrent.top = 0;
		m_rCurrent.right = m_rCurrent.bottom = m_szOneBitmap.cx;
	}
	else if( OneBitmapWidth * OneBitmapHeight == 0 )
		return FALSE;
	else
	{
		// I assume that all single bitmaps' height is not equal to it's width
		m_szOneBitmap.cx = OneBitmapWidth;
		m_szOneBitmap.cy = OneBitmapHeight;
	}
//to set the value of m_dwX, m_dwY
	m_dwX = x; m_dwY = y;

	SetState(0);

	return TRUE;
}


// to preload this bitmap file data by filename 
//BOOL CDDBitmap::PreLoad(LPCTSTR lpszFilename, int x, int y,
//						int OneBitmapWidth/*=0*/, int OneBitmapHeight/*=0*/)
/*
{
	char	fname[20];

	HRESULT			ddrval;
	DDSURFACEDESC	ddsd;

	strcpy( fname, lpszFilename );

	if( !m_BitmapSurface.LoadBitmap(lpszFilename, FALSE) )
	{
		ErrorMessage( hwndGame, INTERFACE_ERROR+10,"Cannot find or open bitmap data file: ",fname );
		return FALSE;
	}
    //
    // get size of surface.
    //
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;

    ddrval = m_BitmapSurface.GetSurface()->GetSurfaceDesc(&ddsd);
	if(ddrval != DD_OK)
		return FALSE;
	m_rTotal.left = m_rTotal.top = 0;
	m_rTotal.right = ddsd.dwWidth;
	m_rTotal.bottom = ddsd.dwHeight;
// to set the value of m_szOneBitmap
	if( (OneBitmapWidth==0)&&(OneBitmapHeight==0) )
	{
		// I assume that all single bitmaps' height is equal to it's width
		if(m_rTotal.bottom > m_rTotal.right)
			m_szOneBitmap.cx = m_szOneBitmap.cy = m_rTotal.right;
		else
			m_szOneBitmap.cx = m_szOneBitmap.cy = m_rTotal.bottom;
		// to set the value of m_rCurrent
		m_rCurrent.left = m_rCurrent.top = 0;
		m_rCurrent.right = m_rCurrent.bottom = m_szOneBitmap.cx;
	}
	else if( OneBitmapWidth * OneBitmapHeight == 0 )
		return FALSE;
	else
	{
		// I assume that all single bitmaps' height is not equal to it's width
		m_szOneBitmap.cx = OneBitmapWidth;
		m_szOneBitmap.cy = OneBitmapHeight;
	}
//to set the value of m_dwX, m_dwY
	m_dwX = x; m_dwY = y;

	SetState(0);

	return TRUE;
}
*/
// to set bitmaps' id
void CDDBitmap::SetID(int ID)
{
	m_nID = ID ;	// button id
}

// to release this button's pointer
void CDDBitmap::Release()
{
	// release the surface that this button's bitmap used
	m_BitmapSurface.Release();
}
// to set bitmaps' state
void CDDBitmap::SetState(int state)	// let button up or down or ...
{
	m_nState = state ;

	if( m_szOneBitmap.cx == m_rTotal.right )
	{
		m_rCurrent.left = m_rTotal.left;
		m_rCurrent.top = m_rTotal.top + m_nState * m_szOneBitmap.cy;
		m_rCurrent.right = m_rCurrent.left + m_szOneBitmap.cx;
		m_rCurrent.bottom = m_rCurrent.top + m_szOneBitmap.cy;
	}
	else
	{
		m_rCurrent.left = m_rTotal.left + m_nState * m_szOneBitmap.cx;
		m_rCurrent.top = m_rTotal.top;
		m_rCurrent.right = m_rCurrent.left + m_szOneBitmap.cx;
		m_rCurrent.bottom = m_rCurrent.top + m_szOneBitmap.cy;
	}
}
// to modify the show position of this bitmap
void CDDBitmap::SetPosition(int x, int y)
{
	m_dwX = x;
	m_dwY = y;
}
// to get the show position of this bitmap
POINT CDDBitmap::GetPosition(void)
{
	POINT	ShowPosition;

	ShowPosition.x = m_dwX;
	ShowPosition.y = m_dwY;

	return ShowPosition;
}
/////////////////////////////////////////
// to show the bitmap
void CDDBitmap::Show()
{
	Blit();
	Update();
}
///////////////////////////////////////////
// to blit whole the bitmap to the back buffer
void CDDBitmap::Blit()
{
	POINT	ptDest;

	ptDest.x = m_dwX;
	ptDest.y = m_dwY;

	m_BitmapSurface.BltToBack(ptDest, &m_rCurrent);
}
///////////////////////////////////////////
//	to blit a compatible part of this bitmap to back buffer by the state
void CDDBitmap::Blit( int ID )
{
	POINT	ptDest;

	ptDest.x = m_dwX;
	ptDest.y = m_dwY;

	if( m_szOneBitmap.cx == m_rTotal.right )
	{
		m_rCurrent.left = m_rTotal.left;
		m_rCurrent.top = m_rTotal.top + (ID - 1) * m_szOneBitmap.cy;
		m_rCurrent.right = m_rCurrent.left + m_szOneBitmap.cx;
		m_rCurrent.bottom = m_rCurrent.top + m_szOneBitmap.cy;
	}
	else
	{
		m_rCurrent.left = m_rTotal.left + (ID - 1) * m_szOneBitmap.cx;
		m_rCurrent.top = m_rTotal.top;
		m_rCurrent.right = m_rCurrent.left + m_szOneBitmap.cx;
		m_rCurrent.bottom = m_rCurrent.top + m_szOneBitmap.cy;
	}

	m_BitmapSurface.BltToBack(ptDest, &m_rCurrent);
}
///////////////////////////////////////////
//	to blit the correctly rectangle of the source bitmap to the ancient destination point of the back buffer
//
// pRect -> the rectangle pointer of the part of the whole bitmap
//	just used to blit the general face from the zzj_face.bmp to the just ponint of the back buffer
void CDDBitmap::Blit( RECT* pRect )
{
	POINT	ptDest;
	RECT	rSource;

	ptDest.x = m_dwX ;
	ptDest.y = m_dwY ;

	rSource.left = pRect->left ;
	rSource.right = pRect->right ;
	rSource.top = pRect->top ;
	rSource.bottom = pRect->bottom ;

	m_BitmapSurface.BltToBack(ptDest, &rSource);
}
//////////////////////////////////////////////////
// to blit the correctly rectangle part of the source bitmap to a new destination point of the back buffer
// 
//pDest -->	the destinate point in the back buffer to blit this rect of bitmap
//pRect --> the rectangle of bitmap should be blit on the back buffer
void CDDBitmap::Blit( POINT* pDest, RECT* pRect )
{
	POINT	ptDest;
	RECT	rSource;

	ptDest.x = pDest->x ;
	ptDest.y = pDest->y ;

	rSource.left = pRect->left - m_dwX;
	rSource.right = pRect->right - m_dwX;
	rSource.top = pRect->top - m_dwY ;
	rSource.bottom = pRect->bottom - m_dwY ;

	m_BitmapSurface.BltToBack(ptDest, &rSource);
}
///////////////////////////////////////////////
//	to update the area of this bitmap file stay
void CDDBitmap::Update()
{
	RECT	ShowRect;
	ShowRect.left = m_dwX,	ShowRect.top = m_dwY;
	ShowRect.right = m_dwX + m_szOneBitmap.cx;
	ShowRect.bottom = m_dwY + m_szOneBitmap.cy;
	DDC_UpdateScreen( &ShowRect );
}
//////////////////////////////////////////////////////
void	FACE_DeleteAllBitmap()
{
	for( int i=0; i<MAX_BITMAP; i++ )
	{
		if( pAllBitmap[i] != NULL	)
		{
			delete pAllBitmap[i];
			pAllBitmap[i] = NULL;
		}
	}
	return;
}
//////////////////////////////////////////////////////
void	FACE_BlitAllBitmap()
{
	for( int i=0; i<MAX_BITMAP; i++ )
	{
		if( pAllBitmap[i] != NULL	)
		{
			pAllBitmap[i]->Blit() ;
		}
	}
	return;
}
//////////////////////////////////////////////////////
